package wps.util {
import mx.collections.ArrayCollection;
import mx.controls.Alert;

import wps.constant.IconXMLConst;
import wps.model.Icon;
import wps.model.IconCategory;

public class CategoryBuilder implements IIconBuilder {

    //--------------------------------------------------------------------------
    //
    // Properties
    //
    //--------------------------------------------------------------------------

    //----------------------------------
    // categories
    //----------------------------------

    [ArrayElementType("wps.model.IconCategory")]
    private var _categories:ArrayCollection;
	
	//----------------------------------
	// categories name
	//----------------------------------
	
	private var _categoriesName:ArrayCollection;

    //--------------------------------------------------------------------------
    //
    // Methods
    //
    //--------------------------------------------------------------------------

    //----------------------------------
    // getResultCollection
    //----------------------------------

    /**
     * get the collection of IconCategory
     */
    public function getResultCollection():ArrayCollection {
        return _categories;
    }
	
	//----------------------------------
	// getCategoryNameCollection
	//----------------------------------
	
	public function getCategoryNameCollection():ArrayCollection {
		return _categoriesName;
	}

    //----------------------------------
    // builder
    //----------------------------------

    public function build(xmlStr:String):void {
        var xml:XML = new XML(xmlStr);

        // path
        var path:String = xml[IconXMLConst.PATH];

        // categories
        var categoriesXML:XML = xml[IconXMLConst.CATETORIES][0];
        if (categoriesXML == null) {
            throw new Error(IconXMLConst.ERR_BUILDER);
        }

        _categories = new ArrayCollection();
		_categoriesName = new ArrayCollection();
        for each (var categoryXML:XML in categoriesXML[IconXMLConst.CATETORY]) {
            // category
            var category:IconCategory = new IconCategory();
            category.name = categoryXML.name;
			_categoriesName.addItem(category.name);

            // icons
//			for each (var iconXML:XML in categoryXML[IconXMLConst.ICONS][IconXMLConst.ICON]) {
//				var icon:Icon = new Icon();
//				icon.name = iconXML.toString();
//				icon.url = path + icon.name;
//				
//				category.iconList.addItem(icon);
//			}

            _categories.addItem(category);
        }
    }

}

}
